﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.member.order
{
    public partial class NewOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxFunction));
            cbuser.dealcookie();
            if (AliApi.appUserId == string.Empty)
            {
                Response.Redirect("/member/login.aspx?redirecturl="+Request.Url.ToString () );
            }

            if (!IsPostBack)
            {
                BindItemInfo();
                BindAddress();
                InitAreaDropDownLists();
            }
        }

        #region 辅助方法
        private void BindAddress()
        {
            PostAddressRadioButtonList.Items.Clear();
            if (AliApi.appUserId != string.Empty)
            {
                cbpostaddress address = cbpostaddress.Find("cbuserid='" + AliApi.appUserId + "'");
                if (address.Count > 0)
                {
                    for (int i = 0; i < address.Count; i++)
                    {
                        string text = address.cbname.Value + " " + address.省编码.Value + " " + address.县编码.Value + " " + address.区编码.Value + " " + address.区域编码 .Value + " " + address.地址.Value;
                        PostAddressRadioButtonList.Items.Add(new ListItem(text,address.RECID.ToString () ));
                        address.Next();
                    }
                }
                PostAddressRadioButtonList.Items.Add(new ListItem("使用其他地址", ""));
            }
            PostAddressRadioButtonList.SelectedIndex = 0;
            PostAddressRadioButtonList_SelectedIndexChanged(PostAddressRadioButtonList, EventArgs.Empty);
        }

        private void BindItemInfo()
        {
            if (Request["id"] == null)
            {
                MessageBox.ShowAndRedirect(this.Page,"对不起，您没有权限访问此页面！","/default.aspx");
            }
            else
            {
                taobaouseritems useritems = taobaouseritems.Find("recid="+Request["id"]);
                if (useritems.Count > 0)
                {
                    //cbuser user = cbuser.Find("cbuserid='" + useritems.taobaoUserId.Value + "'");
                    if (useritems.taobaoUserId.Value!=string.Empty )
                    {
                        ItemImage.ImageUrl = useritems. taobaopic_path .Value;
                        ItemNameHyperLink.Text = useritems.taobaotitle.Value;
                        ItemNameHyperLink.NavigateUrl = taobaouseritems.getitemlinkstringcbw(useritems.taobaoiid.Value);
                        ItemPriceLabel.Text = useritems.taobaoprice.Value;
                        SellerAddLabel.Text = useritems.taobaostate.Value +" "+useritems.taobaocity.Value ;
                        SellerNickLabel.Text = useritems.taobaoNickName.Value;
                        SellerChatLinkLiteral .Text = cbuser.getChatStrfromuserid(useritems.taobaoUserId.Value);

                        //隐藏信息
                        seller_useridHidden.Value = useritems.taobaoUserId.Value;
                        itemiidHidden.Value = useritems.taobaoiid.Value;
                        //物流费用信息
                        for (int i = 0; i < PostTypeRadioButtonList.Items.Count; i++)
                        {
                            switch (PostTypeRadioButtonList.Items[i].Value)
                            {
                                case "EMS" :
                                    {
                                        PostTypeRadioButtonList.Items[i].Text = "EMS：" + useritems.taobaoems_fee.Value + " 元";
                                        break;
                                    }
                                case "快递":
                                    {
                                        PostTypeRadioButtonList.Items[i].Text = "快递：" + useritems.taobaoexpress_fee.Value + " 元";
                                        break;
                                    }
                                case "平邮":
                                    {
                                        PostTypeRadioButtonList.Items[i].Text = "平邮：" + useritems.taobaopost_fee .Value + " 元";
                                        break;
                                    }
                                case "货到付款":
                                    {
                                        PostTypeRadioButtonList.Items[i].Text = "货到付款：0.0 元" ;
                                        break;
                                    }
                                default:
                                    {
                                        break;
                                    }
                            }
                        }

                        PostTypeRadioButtonList_SelectedIndexChanged(PostAddressRadioButtonList, EventArgs.Empty);
                    }
                }
            }
        }

        private void InitAreaDropDownLists()
        {
            this.ProvinceDropDownList.Items.Clear();
            this.ProvinceDropDownList.Items.Add(new ListItem("请您选择省", ""));
            省 province = 省.Find("1=1");
            this.ProvinceDropDownList.DataSource = province.MyTable;
            this.ProvinceDropDownList.DataBind();
            this.ProvinceDropDownList.SelectedIndex = 0;
            ProvinceDropDownList_SelectedIndexChanged(this.ProvinceDropDownList, EventArgs.Empty);
        }
        #endregion

        #region 事件
        protected void PostAddressRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PostAddressRadioButtonList.SelectedValue == "")
            {
                this.OtherAddressDiv.Visible = true;
            }
            else
            {
                this.OtherAddressDiv.Visible = false;
            }
        }
        

        protected void PostTypeRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PostTypeRadioButtonList.SelectedIndex >= 0)
            {
                string text = PostTypeRadioButtonList.SelectedItem.Text;
                int index = text.IndexOf("：");
                int lastIndex = text.IndexOf("元");
                string priceText = text.Substring(index+1, lastIndex - index-"元".Length );
                double postprice =0.0;
                int count = 0;
                double singleprice =0.0;                
                try 
                {
                    postprice = Convert.ToDouble(priceText);
                    count = Convert.ToInt32(ItemCountTextBox.Text);
                    singleprice =Convert.ToDouble (ItemPriceLabel.Text );
                }
                catch 
                {
                }
                TotalPriceLabel.Text = ((singleprice * count) + postprice).ToString(); 

            }
        }
        #endregion

        #region 地区下拉框事件
        protected void CityDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DisctictDropDownList.Items.Clear();
            this.DisctictDropDownList.Items.Add(new ListItem("请选择区", ""));
            if (this.CityDropDownList.SelectedValue != string.Empty)
            {
                区 disctict = 区.Find("县编码='" + this.CityDropDownList.SelectedValue + "'");
                this.DisctictDropDownList.DataSource = disctict.MyTable;
                this.DisctictDropDownList.DataBind();
            }
            this.DisctictDropDownList.SelectedIndex = 0;
            DisctictDropDownList_SelectedIndexChanged(this.DisctictDropDownList, EventArgs.Empty);
        }

        protected void ProvinceDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CityDropDownList.Items.Clear();
            this.CityDropDownList.Items.Add(new ListItem("请选择市", ""));
            if (this.ProvinceDropDownList.SelectedValue != string.Empty)
            {
                县 city = 县.Find("省编码='" + this.ProvinceDropDownList.SelectedValue + "'");
                this.CityDropDownList.DataSource = city.MyTable;
                this.CityDropDownList.DataBind();
            }
            this.CityDropDownList.SelectedIndex = 0;
            CityDropDownList_SelectedIndexChanged(this.CityDropDownList, EventArgs.Empty);
        }

        protected void DisctictDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.AreaDropDownList.Items.Clear();
            this.AreaDropDownList.Items.Add(new ListItem("请选择区域", ""));
            if (this.DisctictDropDownList.SelectedValue != string.Empty)
            {
                区域 area = 区域.Find("县编码='" + this.CityDropDownList.SelectedValue + "' and 区编码='" + this.DisctictDropDownList.SelectedValue + "'");
                this.AreaDropDownList.DataSource = area.MyTable;
                this.AreaDropDownList.DataBind();
            }
            this.AreaDropDownList.SelectedIndex = 0;
        }
        #endregion

        /// <summary>
        /// 确定订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ConfirmBuyButton_Click(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                taobaotrade trade = taobaotrade.Find("1=2");
                trade.New();
                trade.seller_nick.Value = seller_useridHidden.Value;
                trade.buyer_nick.Value = AliApi.appUserId;
                trade.taobaoiid.Value = itemiidHidden.Value;
                trade.taobaoname.Value = ItemNameHyperLink.Text;
                trade.taobaoprice.Value = ItemPriceLabel.Text;
                trade.taobaopic_path.Value = ItemImage.ImageUrl;
                trade.taobaonum.Value = Convert.ToInt32(ItemCountTextBox.Text);
                trade.taobaocreated.Value = DateTime.Now.ToString();
                trade.taobaotid.Value = trade.GetRecID().ToString ();
                trade.taobaopayment.Value = TotalPriceLabel.Text;
                trade.taobaostatus.Value = TRADE_STATUS.Buyer_Order;
                trade.taobaoseller_rate.Value = "false";
                trade.taobaobuyer_rate.Value = "false";
                trade.Notes.Value = LiuYanTextBox.Text;

                string tranStr = PostTypeRadioButtonList.SelectedItem.Text ;
                int startIndex = tranStr.IndexOf("：");
                int endIndex = tranStr.IndexOf("元");

                trade.cbtrantype.Value = tranStr.Substring (0,startIndex);
                trade.transfee.Value = tranStr.Substring(startIndex +1,endIndex -1-startIndex -1) ;
                trade.Insert();

                if (PostAddressRadioButtonList.SelectedValue == "")
                {
                    cbpostaddress address = cbpostaddress.Find("1=2");
                    address.New();
                    address.cbuserid.Value = AliApi.appUserId;
                    address.省编码.Value = ProvinceDropDownList.SelectedValue;
                    address.县编码.Value = CityDropDownList.SelectedValue;
                    address.区编码.Value = DisctictDropDownList.SelectedValue;
                    address.区域编码.Value = AreaDropDownList.SelectedValue;
                    address.邮政编码.Value = PostCodeTextBox.Text;
                    address.地址.Value = DetailAddressTextBox.Text;
                    address.cbname.Value = ReceiveNameTextBox.Text;
                    address.Phone.Value = PhoneTextBox.Text;
                    address.PhoneLocal.Value = PhoneLocalTextBox.Text;
                    address.PhoneMobile.Value = PhoneMobileTextBox.Text;
                    address.Insert();
                }

                MessageBox.ShowAndRedirect(this.Page, "祝贺您，下单成功！", "OrderResult.aspx?id=" + Request["id"]);
            }
        }


    }
}
